package utils;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: Project
 * @className DBUtils
 * @description:
 * @author: DZH
 * @create: 2023-09-20 14:41
 * @Version 1.0
 **/
public class DBUtils {

    //公用的增删改方法
    public static int publicUpdate(String sql,Object...args){
        //获取连接
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            connection = JdbcUtils.getConnection();
            ps = connection.prepareStatement(sql);
            //充填占位符
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1,args[i]);
            }

            //执行操作
            int i = ps.executeUpdate();

            return i;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭连接
            JdbcUtils.close(connection,ps);
        }

    }

    //公共的查询方法
    public static <T> T updateQuery(Class<T> clazz,String sql,Object...args){


        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            connection = JdbcUtils.getConnection();
            ps = connection.prepareStatement(sql);
            //添加占位符
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1,args[i]);
            }
            resultSet = ps.executeQuery();
            if (resultSet.next()){
                T t = clazz.newInstance();
                ResultSetMetaData metaData = resultSet.getMetaData();
                int columnCount = metaData.getColumnCount();

                for (int i = 0; i < columnCount; i++) {
                    String key = metaData.getColumnLabel(i + 1);
                    Object value = resultSet.getObject(i + 1);
                    Field declaredField = t.getClass().getDeclaredField(key);
                    declaredField.setAccessible(true);
                    declaredField.set(t,value);
                }

                return t;
            }else{
                return null;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } finally {
            JdbcUtils.close(connection,ps,resultSet);
        }


    }


    //查询多条数据
    public static <T> List<T> publicQueryList(Class<T> clazz,String sql,Object...args){

        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        List<T> listData=new ArrayList<>();

        try {
            connection = JdbcUtils.getConnection();
            ps = connection.prepareStatement(sql);
            //添加占位符
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1,args[i]);
            }
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                T t = clazz.newInstance();
                ResultSetMetaData metaData = resultSet.getMetaData();
                int columnCount = metaData.getColumnCount();

                for (int i = 0; i < columnCount; i++) {
                    String key = metaData.getColumnLabel(i + 1);
                    Object value = resultSet.getObject(i + 1);
                    Field declaredField = t.getClass().getDeclaredField(key);
                    declaredField.setAccessible(true);
                    declaredField.set(t, value);
                }
                listData.add(t);

            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } finally {
            JdbcUtils.close(connection,ps,resultSet);
        }

        return listData;
    }
}
